package com.linsy.product.redis.mq.util;

import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 异步执行消费的线程池
 */
public class KernelMqThreadPool {

    private KernelMqThreadPool() {
    }


    @Bean("kernel-mq-async-executor")
    public Executor getExecutor() {
        int cpuNum = Runtime.getRuntime().availableProcessors();
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(2 * cpuNum);//核心线程大小
        executor.setMaxPoolSize(4 * cpuNum);//最大线程数
        executor.setQueueCapacity(50);//最大队列数
        executor.setKeepAliveSeconds(20);//非核心线程空闲时间
        executor.setThreadNamePrefix("mq-pool");//线程前缀名称
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());//拒绝策略
        return executor;
    }

}
